﻿/**
 * loading文档类
 * 
 * @author Luodong
 * @email luo4521@online.net.tw
 * 2010-2-2 10:19
 */
package com.addcn.texas
{
	import com.addcn.common.data.UserInfoData;
	import com.addcn.common.utils.GameLoader;
	import com.addcn.common.utils.QuenceLoader;
	import com.addcn.common.event.DataEvent;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.data.Global;
	import com.addcn.common.data.ServerInfo;
	
	import flash.display.MovieClip;	
	import flash.display.Sprite;	
	import flash.system.ApplicationDomain;
	
	public class LoadingMain extends Sprite
	{
		private var _load_mc : MovieClip;
		private var _app : ApplicationDomain;
		private var _golobal : Global;
		private var _userInfo : UserInfoData;
		private var _bytes_total : Number;
		private var _bytes_loaded : Number;
		
		function LoadingMain() : void {
			initialize() ;
			getPagrams();			
			loadXmlConfig();
		}
				
		private function initialize() : void 
		{
			this._golobal = Global.getInstance();
			this._userInfo = UserInfoData.getInstance();
			this._bytes_total  = 0;
			this._bytes_loaded = 0;
		}
		
		/**
		 * 獲取頁面傳遞過來的參數，保存到全局变量
		 * 
		 */
		private function getPagrams() : void
		{			
			var _params : Object = this.loaderInfo.parameters;
			_golobal.version = _params["v"];
			_userInfo.id = parseInt(_params["id"]);
			_userInfo.key = _params["key"];			
		}		
	
		//加载xml配置文件
		private function loadXmlConfig() : void 
		{			
			var _url : String = _golobal.xmlUrl;
			_app = new ApplicationDomain(ApplicationDomain.currentDomain);
			var _loader : GameLoader = new GameLoader(_app);  //實例化加載器
			_loader.add(_url, { id:"startXml", name:Global.GAME_NAME, type:"xml"} );  //添加xml路徑到加載隊列中
			_loader.addEventListener(GameLoader.COMPLETE, loadXmlComplete);
			_loader.start();//開始加載
		}
		
		/**
		 * 加載xml配置文件完成，解析内容
		 * @param	evt 
		 */
		private function loadXmlComplete(evt : DataEvent) : void 
		{
			evt.target.removeEventListener(GameLoader.COMPLETE, loadXmlComplete);			
			var _xml_data : XML = XML(evt.target.getFile("startXml"));	
			_analyseSeverData(_xml_data);  //分析服務器配置數據
			var _module_loader : QuenceLoader = new QuenceLoader(_app);	//實例化加載器,用于加載module	
			var _url : String = _xml_data.child("info").child("loadingUi")
			_module_loader.add(_url, { id:"loadingUi", name:"loadingUi" } ); //将要加载的模块加入加载队列				
			var _element : Object = _xml_data.child("moduleList").child("module");			
			for each(var _val in _element) {  //遍配置文件歷模塊列表 
				_module_loader.add(_val, { id:String(_val.@id), name:String(_val.@name) } );  //将模块加入下載隊列
				this._bytes_total += Number(_val.@size);
			}				
			_module_loader.addEventListener(GameLoader.COMPLETE, loadModuleComplete);	
			_module_loader.addEventListener(GameLoader.PROGRESS, loadModuleProgress);
			_module_loader.addEventListener(GameLoader.IO_ERROR, loadModuleError);
			_module_loader.start();//開始加載loadingUi
		}
		
		private function loadModuleComplete(evt : DataEvent) : void 
		{			
			
			var _name = evt.data.name;
			if ("loadingUi" == _name) {				
				this._load_mc = evt.target.getFile("loadingUi").content;	
				_load_mc.Loading.setTotalBytes(this._bytes_total);
				this.addChild(this._load_mc);
			}
			//所有模块加载完成
			if (evt.data.com == 0 ) {					
					var _shell = evt.target.getFile("shell");
					_shell.x = 0;
					_shell.y = 0;
					this.removeChild(this._load_mc);
					_removeListener(evt.target);
					this.addChild(_shell);				
			}
		}
		
		private function loadModuleProgress(evt : DataEvent) : void 
		{
			if(this._load_mc){
				this._bytes_loaded += evt.data.loaded;				
				this._load_mc.Loading.updateLoaded(this._bytes_loaded);
			}
		}
		
		private function loadModuleError(evt : DataEvent) : void 
		{
			trace("load error");
		}
				
		private function _analyseSeverData(r_xml_data : XML) : void {
			
			var _server : ServerInfo = ServerInfo.getInstance();
			if (r_xml_data.child("info").child("debug") == "true") {
				Global.getInstance().debug = true;
			}			
			var _parent :XMLList = r_xml_data.child("server");
			var _ip : String = _parent.child("ip")
			var _port : int  = parseInt(_parent.child("port"));
			var _zone : String = _parent.child("zone");
			var _ext : Array = [];			
			var _list : XMLList = _parent.child("extList").child("ext");			
			for each(var _val in _list) {
				_ext.push(_val);		
			}	
			_server.setInfo([_ip, _port, _zone, _ext]);
			
			//_server.serverIp = r_xml_data.child("server").child("ip");			
			//ServerInfo.getInstance().serverPort = int(r_xml_data.child("server").child("port"));
			//ServerInfo.getInstance().zone = r_xml_data.child("server").child("zone");
			
			if(String(r_xml_data.child("photoUrl"))){
				Global.getInstance().photoUrl = String(r_xml_data.child("photoUrl"));		
			}
		}
		
		private function _removeListener(r_targe : Object) : void
		{
			r_targe.removeEventListener(GameLoader.COMPLETE, loadModuleComplete);	
			r_targe.removeEventListener(GameLoader.PROGRESS, loadModuleProgress);
			r_targe.removeEventListener(GameLoader.IO_ERROR, loadModuleError);
		}
		

	}
	
}
